package cn.mxb.logic.action.mxb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.SysZu;
import cn.mxb.logic.entity.MxbCard;
import cn.mxb.logic.entity.MxbPolicy;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysUserService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.ISysZuService;
import unit.IdGenerator;
import unit.MyTime;
import cn.mxb.logic.service.IMxbCardService;
import cn.mxb.logic.service.IMxbPolicyService;

@SuppressWarnings({ "serial", "rawtypes" })
public class MxbCardAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/mxb/mxbCardList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/mxb/mxbCardAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/mxb/mxbCardUpdate.jsp";
	protected static final String MXBCARDREGISTRATIONADDJSP = "/WEB-INF/page/web/mxb/mxbCardregistrationAdd.jsp";
	
	
	private MxbCard mxbCard;
	@Autowired
	private IMxbCardService mxbCardService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysZuService sysZuService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IMxbPolicyService mxbPolicyService;
	
	private Result<MxbCard> pageResult;
	
	private List<MxbCard> mxbCardAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	private List<SysZu> sysZuAllList; 
	private List<SysUser> sysUserList; 
	private List<MxbPolicy> mxbPolicicyAllList;
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();    //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();     //镇名
	private Map<Integer,String> villageNameMap = new HashMap<Integer, String>();  //村名
	private Map<Integer,String> zUidMap = new HashMap<Integer, String>(); 
	private Map<String,String> userNameMap = new HashMap<String, String>(); 
	private Map<String,String> mxbNameMap = new HashMap<String, String>(); 
	private String familyNameY; //参合家庭姓名搜索关键字
	
	
	/**
	 * 跳转慢性病证登记信息
	 * @return
	 */
	public String openregistration(){
		if(mxbCard.getUserId() !=null && !"".equals(mxbCard.getUserId())){
			sysUserList	= sysUserService.findListByProperty("userId",mxbCard.getUserId());
			System.out.println("sysuserlist"+sysUserList.get(0).getUserRealName());
		}
		mxbPolicicyAllList = mxbPolicyService.findAllList();
		mxbCard = mxbCardService.findByProperty("id", mxbCard.getId()+"");
		setForwardView(MXBCARDREGISTRATIONADDJSP);
		return SUCCESS;
	}
	/**
	 * 慢性病证 信息登记
	 * @return
	 */
	public String Mxbopenregistration(){
		try{
			System.out.println("update");
			mxbCardService.update(mxbCard);
		}catch(Exception e){
			System.out.println(e);
		}
		
		return openList();
		
	}
	
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		sysZuAllList = sysZuService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @throws Exception 
	 * @return_type: String
	 */
	public String add() throws Exception{
		System.out.println("add");
		/*IdGenerator id = new IdGenerator(1023);
		mxbCard.setFamilyId((id.getNextId()).intValue());*/
		
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = mxbCardService.find(mxbCard, getPage(), getRow());
			for(MxbCard mxbCard : pageResult.getData()){
				if(null != mxbCard.getUserId()  && !"".equals(mxbCard.getUserId())){
					userNameMap.put(mxbCard.getUserId(),sysUserService.findByProperty("userId", mxbCard.getUserId()).getUserRealName());
				}
				if(null != mxbCard.getMxbId() && !"".equals(mxbCard.getMxbId())){
					mxbNameMap.put(mxbCard.getMxbId(),mxbPolicyService.findByProperty("mxbId", mxbCard.getMxbId()).getMxbName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		mxbCardService.delete(mxbCard.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		mxbCard =  mxbCardService.findByProperty("id",mxbCard.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		mxbCardService.add(mxbCard);
		mxbCard = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return mxbCard;
	}
	@Override
	public void prepare() throws Exception {
		if(mxbCard ==null){
			mxbCard = new MxbCard();
		}
	}


	public MxbCard getMxbCard() {
		return mxbCard;
	}

	public void setMxbCard(MxbCard mxbCard) {
		this.mxbCard = mxbCard;
	}

	public Result<MxbCard> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<MxbCard> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<Integer, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<MxbCard> getMxbCardAllList() {
		return mxbCardAllList;
	}

	public void setMxbCardAllList(List<MxbCard> mxbCardAllList) {
		this.mxbCardAllList = mxbCardAllList;
	}

	public List<SysZu> getSysZuAllList() {
		return sysZuAllList;
	}

	public void setSysZuAllList(List<SysZu> sysZuAllList) {
		this.sysZuAllList = sysZuAllList;
	}

	public Map<Integer, String> getzUidMap() {
		return zUidMap;
	}

	public void setzUidMap(Map<Integer, String> zUidMap) {
		this.zUidMap = zUidMap;
	}

	public String getFamilyNameY() {
		return familyNameY;
	}

	public void setFamilyNameY(String familyNameY) {
		this.familyNameY = familyNameY;
	}

	public Map<String, String> getUserNameMap() {
		return userNameMap;
	}

	public void setUserNameMap(Map<String, String> userNameMap) {
		this.userNameMap = userNameMap;
	}

	public Map<String, String> getMxbNameMap() {
		return mxbNameMap;
	}

	public void setMxbNameMap(Map<String, String> mxbNameMap) {
		this.mxbNameMap = mxbNameMap;
	}

	public List<SysUser> getSysUserList() {
		return sysUserList;
	}

	public void setSysUserList(List<SysUser> sysUserList) {
		this.sysUserList = sysUserList;
	}

	public List<MxbPolicy> getMxbPolicicyAllList() {
		return mxbPolicicyAllList;
	}

	public void setMxbPolicicyAllList(List<MxbPolicy> mxbPolicicyAllList) {
		this.mxbPolicicyAllList = mxbPolicicyAllList;
	}

	


	

	
	
}
